/// 过滤级别
enum FilterLevel {
  none,     // 不过滤
  basic,    // 基础过滤（过滤明显成人内容）
  strict,   // 严格过滤（过滤所有可能不适宜内容）
}

/// 内容过滤工具类
/// 用于过滤成人内容和不适宜的内容
class ContentFilter {
  /// 成人内容的rating标识
  static const List<String> _adultRatings = [
    'R+ - Mild Nudity',
    'Rx - Hentai',
    'R - 17+ (violence & profanity)',
  ];

  /// 不适宜展示的内容类型ID (根据MAL genre ID)
  static const List<int> _adultGenreIds = [
    9, // Ecchi
    49, // Erotica  
    12, // Hentai
  ];

  /// 成人内容关键词
  static const List<String> _adultKeywords = [
    'hentai',
    'ecchi',
    'adult',
    '成人',
    '限制级',
    '18+',
    'r18',
    'nsfw',
  ];

  /// 检查动漫是否为成人内容
  static bool isAdultContent(
    String? rating,
    List<String> genres,
    String title,
    String synopsis,
    {FilterLevel level = FilterLevel.basic}
  ) {
    switch (level) {
      case FilterLevel.none:
        return false;
      
      case FilterLevel.basic:
        return _isBasicAdultContent(rating, genres, title, synopsis);
      
      case FilterLevel.strict:
        return _isStrictAdultContent(rating, genres, title, synopsis);
    }
  }

  /// 基础成人内容检查
  static bool _isBasicAdultContent(
    String? rating,
    List<String> genres,
    String title,
    String synopsis,
  ) {
    // 检查rating
    if (rating != null && _adultRatings.contains(rating)) {
      return true;
    }

    // 检查类型（通过名称匹配）
    final lowerGenres = genres.map((g) => g.toLowerCase()).toList();
    if (lowerGenres.any((genre) => 
        genre.contains('hentai') || 
        genre.contains('ecchi') || 
        genre.contains('erotica'))) {
      return true;
    }

    // 检查标题关键词
    final lowerTitle = title.toLowerCase();
    if (_adultKeywords.any((keyword) => 
        lowerTitle.contains(keyword.toLowerCase()))) {
      return true;
    }

    return false;
  }

  /// 严格成人内容检查
  static bool _isStrictAdultContent(
    String? rating,
    List<String> genres,
    String title,
    String synopsis,
  ) {
    // 基础检查
    if (_isBasicAdultContent(rating, genres, title, synopsis)) {
      return true;
    }

    // 更严格的rating检查
    if (rating != null && (
        rating.contains('R+') || 
        rating.contains('R -') ||
        rating.contains('17+'))) {
      return true;
    }

    // 检查synopsis中的成人内容关键词
    final lowerSynopsis = synopsis.toLowerCase();
    if (_adultKeywords.any((keyword) => 
        lowerSynopsis.contains(keyword.toLowerCase()))) {
      return true;
    }

    return false;
  }

  /// 过滤动漫列表
  static List<T> filterAnimeList<T>(
    List<T> animeList,
    FilterLevel level,
    String? Function(T) getRating,
    List<String> Function(T) getGenres,
    String Function(T) getTitle,
    String Function(T) getSynopsis,
  ) {
    if (level == FilterLevel.none) {
      return animeList;
    }

    return animeList.where((anime) {
      return !isAdultContent(
        getRating(anime),
        getGenres(anime),
        getTitle(anime),
        getSynopsis(anime),
        level: level,
      );
    }).toList();
  }

  /// 获取过滤级别的中文描述
  static String getFilterLevelDescription(FilterLevel level) {
    switch (level) {
      case FilterLevel.none:
        return '不过滤';
      case FilterLevel.basic:
        return '基础过滤';
      case FilterLevel.strict:
        return '严格过滤';
    }
  }

  /// 获取所有过滤级别
  static List<FilterLevel> getAllFilterLevels() {
    return FilterLevel.values;
  }
} 